//
//  NSAttributedString+Extension.swift
//  WeiBo
//
//  Created by Feng on 2017/6/16.
//  Copyright © 2017年 feng. All rights reserved.
//

import UIKit

extension NSAttributedString
{
    class func attributedStringWithEmoticon(emoticon: HMEmoticon, font: UIFont) -> NSAttributedString
    {
        // 1.创建UIImage对象
        let image = UIImage(named: emoticon.path!, in: EmoticonButton, compatibleWith: nil)
        // 2.创建文本附件(NSTextAttachment)
        //            let textAttachment = NSTextAttachment()
        let textAttachment = HMEmoticonTextAttachment()
        textAttachment.emoticon = emoticon
        //获取文字高度
        let lineHeight = font.lineHeight
        //更改富文本的bounds,使文字能够和富文本对齐
        textAttachment.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
        // 3.设置文本附件对应的图片对象
        textAttachment.image = image
        // 4.根据文本附件创建富文本
        let emoticonAttributeText = NSAttributedString(attachment: textAttachment)
        
        return emoticonAttributeText
    }
}
